package com.bear.newsset.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.bear.newsset.R;
import com.bear.newsset.dao.entity.AccountEntity;
import com.bear.newsset.dao.sql.AccountSQL;
import com.bear.newsset.mvp.presen.MainPresen;
import com.bear.newsset.util.AppUtil;
import com.bear.newsset.util.DOM;
import com.bear.newsset.util.LogUtils;
import com.bear.newsset.util.PermManage;
import com.bear.newsset.mvp.view.MainView;
import com.bear.newsset.widget.BaseActivity;
import com.bear.newsset.widget.MainMenuView;
import com.ybear.ybmvp.annotations.Presen;

import static com.bear.newsset.util.Constant.DOM_ID_MENU_VIEW_CHECKED_POSITION;
import static com.bear.newsset.util.Constant.DOM_ID_SET_STATUS_BAR_COLOR;

public class MainActivity extends BaseActivity implements MainView {
    @Presen
    private MainPresen mPresen;
    private MainMenuView mMenuView;
    private ThisHandler mThisHandler;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        AppUtil.doubleBackPressedExit(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //请求所有权限
        PermManage.getInstance( this ).requestPermAll();


        /* test account */
        AccountSQL aSQL = AccountSQL.getInstance();
        if( aSQL.queryAccountTop() == null ) {
            AccountEntity e = aSQL.createTestAccount();
            aSQL.insertAccount( e );
        }
        //设置全局账号
        ((NSApplication)getApplication()).setAccountEntity( aSQL.queryAccountTop() );

        initView();
        initData();
        initListener();
        //版本检查
        mThisHandler.sendEmptyMessage(666);
    }

    /**
     * 初始化View
     */
    private void initView() {
        mMenuView = findViewById(R.id.main_menu_view);
        //切换到首页
        mPresen.switchFragmentHome();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //禁用侧滑退出
        setEnableSwipeBack( false );
        mThisHandler = new ThisHandler(this);
//        设置菜单View的图标和选中图标
        mMenuView.setMenuIcon(
                R.drawable.ic_main_meun_home_def,
                /*R.drawable.ic_main_meun_found_def,*/
                R.drawable.ic_main_meun_my_def
        ).setMenuIconForSelect(
                R.drawable.ic_main_meun_home,
                /*R.drawable.ic_main_meun_found,*/
                R.drawable.ic_main_meun_my
        );
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        //设置菜单选中改变事件监听器
        mMenuView.setOnCheckedChangeListener((view, checkedId, position) -> {
            //设置结果，通知所有注册的监听器
            DOM.getInstance().setResult(DOM_ID_MENU_VIEW_CHECKED_POSITION, position);
            //处理菜单选中改变事件
            doMenuCheckedChanged(checkedId, position);
        });

        //注册观察者监听器
        DOM.getInstance().registerResult((id, data) -> {
            if( id != DOM_ID_SET_STATUS_BAR_COLOR && !( data instanceof Boolean ) ) return;
            setStatusBarIconColor( (boolean) data );
        });
    }

    /**
     * 处理菜单选中改变事件
     * @param checkedId     选中Item的Id
     * @param position      选中Item的位置，从 0 开始
     */
    private void doMenuCheckedChanged(int checkedId, int position) {
        if( mMenuView.getOldPosition() == position ) return;
        switch ( position ) {
            case 0: //切换到首页
                mPresen.switchFragmentHome();
                break;
            case 1: //切换到我的
                mPresen.switchFragmentMe();
                break;
//            case 1: //切换到探索
//                mPresen.switchFragmentExplore();
//                break;
//            case 2: //切换到我的
//                mPresen.switchFragmentMe();
//                break;
        }
        LogUtils.d("doMenuCheckedChanged", "CheckedId:" + checkedId +
                " | Position:" + position +
                " | Old Position:" + mMenuView.getOldPosition());
    }

    @Override
    public MainActivity callMainActivity() {
        return this;
    }

    /**
     * 默认设置状态栏图标为白色
     * @return         状态栏图标颜色
     */
    @Override
    protected STATUS_BAR_ICON_COLOR onStatusBarIconColor() {
        return STATUS_BAR_ICON_COLOR.WHITE;
    }

    /**
     * Handler
     */
    private static class ThisHandler extends Handler {
        private MainActivity mActivity;
        ThisHandler(MainActivity mActivity) {
            super(Looper.getMainLooper());
            this.mActivity = mActivity;
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch ( msg.what ) {
                case 666:   //版本更新检查
                    AppUtil.checkVersion(mActivity);
                    break;
            }
        }
    }
}
